package com.snail.snailvr.views.fragments;

import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;

import com.snail.snailvr.R;
import com.snail.snailvr.model.History;
import com.snail.snailvr.utils.DatabaseUtils;
import com.snail.snailvr.utils.DialogUtils;
import com.snail.snailvr.utils.IntentUtils;
import com.snail.snailvr.views.adapter.HistoryAdapter;
import com.snail.snailvr.widget.HorizontalDividerItemDecoration;
import com.snail.snailvr.widget.WrapContentLinearLayoutManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import rx.Subscriber;
import rx.Subscription;

/**
 * 播放历史界面
 * Created by snail on 16/4/13.
 */
public class HistoryFragment extends BaseToolbarFragment implements View.OnClickListener {

    @Bind(R.id.toolbar)
    Toolbar mToolbar;
    @Bind(R.id.list)
    RecyclerView mRecyclerView;
    @Bind(R.id.empty_view)
    View mEmptyView;

    private Subscription mGetLocalVideosSubscription;
    private List<History> mHistoryList;
    private HistoryAdapter mAdapter;


    @Override
    protected void handleIntent() {

    }

    @Override
    protected void initView() {
        setupToolbar(mToolbar, R.drawable.ic_arrow_down, getString(R.string.video_paly_history), R.menu.menu_history);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new WrapContentLinearLayoutManager(getActivity()));
        mHistoryList = new ArrayList<>();
        mAdapter = new HistoryAdapter(getActivity(), mHistoryList, this,false);
        mRecyclerView.setAdapter(mAdapter);
        addDivider();
        loadData();
    }
    private void addDivider() {
        HorizontalDividerItemDecoration horizontalDividerItemDecoration=new HorizontalDividerItemDecoration.Builder(getActivity())
                .paintProvider(mAdapter)
                .visibilityProvider(mAdapter)
                .marginProvider(mAdapter)
                .build();
        horizontalDividerItemDecoration.setBackgroundPaintColor(getResources().getColor(R.color.list_item_color_normal));
        mRecyclerView.addItemDecoration(horizontalDividerItemDecoration);
    }
    @Override
    protected void loadData() {
        mGetLocalVideosSubscription = DatabaseUtils.getPlayHistory(new Subscriber<History>() {
            @Override
            public void onCompleted() {
                showHistory();
            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onNext(History history) {
                mHistoryList.add(history);
            }
        });
    }

    private void showHistory() {
        if(mHistoryList.isEmpty())
            showEmpty();
        else {
            mAdapter.notifyItemRangeInserted(0, mHistoryList.size());
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_local_videos;
    }

    @Override
    protected boolean onToolbarMenuItemClick(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_clear:
                clearHistory();
                return true;
        }
        return false;
    }

    private void clearHistory() {
        if (mHistoryList != null && !mHistoryList.isEmpty()) {
            DialogUtils.showBasicDialog(getActivity(), getString(R.string.clear_history), getString(R.string.clear_history_content)
                    , getString(R.string.CANCEL), getString(R.string.clear_all), true, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mHistoryList.clear();
                            mAdapter.notifyDataSetChanged();
                            DatabaseUtils.clearHistory();
                            showEmpty();

                        }
                    });
        }

    }

    private void showEmpty() {
        mEmptyView.setVisibility(View.VISIBLE);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mGetLocalVideosSubscription != null && !mGetLocalVideosSubscription.isUnsubscribed())
            mGetLocalVideosSubscription.unsubscribe();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.history_item_layout:
                History history = (History) v.getTag();
                if (history != null) {
                    IntentUtils.playVideo(getActivity(), history.getPath(), history.getName());
                }
                break;
        }
    }
}
